from librpg.menu import Widget
from librpg.image import Image
from librpg.locals import SRCALPHA
from librpg.color import DARK_GREEN, WHITE, BLUE, RED, GRAY
from librpg.config import menu_config

import pygame
from math import pi


class Chalkboard(Widget):
    
    def __init__(self, ingame, width, height):
        Widget.__init__(self, width, height, focusable=False)
        self.ingame = ingame
        
        self.s = pygame.Surface((width, height), SRCALPHA, 32)
        self.image = Image(self.s)
        
        self.pitch_surface = None
        
        self.line_width = width / 100

    def draw(self):        
        if self.pitch_surface is None:
            self.draw_pitch_surface()
            
        s = self.image.surface
        s.blit(self.pitch_surface, (0, 0))
        
        theme = menu_config.theme
        font = theme.get_font(12)
        for color, match_team in zip((BLUE, RED), self.ingame.match_teams):
            for match_player in match_team.match_players:
                if not match_player.is_sent_off():
                    x, y = self.__calc_position(match_player.position)
                    pygame.draw.circle(s, color, (x, y), 10, 0)
                    text = font.render(str(match_player.number), True, WHITE)
                    s.blit(text, (x - text.get_width() / 2, y - text.get_height() / 2))
                    
        x, y = self.__calc_position(self.ingame.ball.position)
        pygame.draw.circle(s, GRAY, (x, y), 7, 0)

    def __calc_position(self, xyz_pos):
        return (int(xyz_pos[0] * self.width), int(xyz_pos[2] * self.height))

    def draw_pitch_surface(self):
        s = pygame.Surface((self.width, self.height), SRCALPHA, 32)
        lw = self.line_width
        w = self.width
        h = self.height
        
        # Fill with grass
        s.fill(DARK_GREEN)
        
        # Borders
        r = pygame.Rect(0, 0, w - 1, h - 1)
        pygame.draw.rect(s, WHITE, r, lw)
        
        # Midfield line
        pygame.draw.line(s, WHITE, (0, h / 2), (w - 1, h / 2), lw)
        
        # Penalty areas
        PENALTY_AREA_PROPORTION_X = 0.5
        PENALTY_AREA_PROPORTION_Y = 0.15
        PENALTY_AREA_DIST_X = ((1 - PENALTY_AREA_PROPORTION_X) / 2) * w
        area = pygame.Rect(PENALTY_AREA_DIST_X, 0,
                           w * PENALTY_AREA_PROPORTION_X,
                           h * PENALTY_AREA_PROPORTION_Y)
        pygame.draw.rect(s, WHITE, area, lw)
        area = pygame.Rect(PENALTY_AREA_DIST_X,
                           h * (1 - PENALTY_AREA_PROPORTION_Y),
                           w * PENALTY_AREA_PROPORTION_X,
                           h * PENALTY_AREA_PROPORTION_Y)
        pygame.draw.rect(s, WHITE, area, lw)
        
        # Goal areas
        GOAL_AREA_PROPORTION_X = 0.2
        GOAL_AREA_PROPORTION_Y = 0.06
        GOAL_AREA_DIST_X = ((1 - GOAL_AREA_PROPORTION_X) / 2) * w
        area = pygame.Rect(GOAL_AREA_DIST_X, 0,
                           w * GOAL_AREA_PROPORTION_X,
                           h * GOAL_AREA_PROPORTION_Y)
        pygame.draw.rect(s, WHITE, area, lw)
        area = pygame.Rect(GOAL_AREA_DIST_X,
                           h * (1 - GOAL_AREA_PROPORTION_Y),
                           w * GOAL_AREA_PROPORTION_X,
                           h * GOAL_AREA_PROPORTION_Y)
        pygame.draw.rect(s, WHITE, area, lw)
        
        # Central circle
        CENTRAL_CIRCLE_RADIUS_PROPORTION_X = 0.15
        CENTRAL_CIRCLE_RADIUS = int(CENTRAL_CIRCLE_RADIUS_PROPORTION_X * w)
        center = (w / 2, h / 2)
        pygame.draw.circle(s, WHITE, center, CENTRAL_CIRCLE_RADIUS, lw)
        
        DOT_RADIUS_PROPORTION_X = 0.015
        DOT_RADIUS = int(DOT_RADIUS_PROPORTION_X * w)
        pygame.draw.circle(s, WHITE, center, DOT_RADIUS, 0)
        
        # Penalty marks
        MARK_RADIUS_PROPORTION_X = 0.01
        MARK_RADIUS = int(MARK_RADIUS_PROPORTION_X * w)
        PENALTY_MARK_Y_OFFSET_PROPORTION = 0.1
        mark1 = (int(w / 2), int(h * PENALTY_MARK_Y_OFFSET_PROPORTION))
        pygame.draw.circle(s, WHITE, mark1, MARK_RADIUS, 0)
        mark2 = (int(w / 2), int(h * (1 - PENALTY_MARK_Y_OFFSET_PROPORTION)))
        pygame.draw.circle(s, WHITE, mark2, MARK_RADIUS, 0)
        
        # Area arcs
        DELTA_ANGLE = 0.5
        r = pygame.Rect(mark1[0] - CENTRAL_CIRCLE_RADIUS,
                        mark1[1] - CENTRAL_CIRCLE_RADIUS,
                        2 * CENTRAL_CIRCLE_RADIUS,
                        2 * CENTRAL_CIRCLE_RADIUS)
        pygame.draw.arc(s, WHITE, r, pi + DELTA_ANGLE, 2 * pi - DELTA_ANGLE, lw)
        r = pygame.Rect(mark2[0] - CENTRAL_CIRCLE_RADIUS,
                        mark2[1] - CENTRAL_CIRCLE_RADIUS,
                        2 * CENTRAL_CIRCLE_RADIUS,
                        2 * CENTRAL_CIRCLE_RADIUS)
        pygame.draw.arc(s, WHITE, r, DELTA_ANGLE, pi - DELTA_ANGLE, lw)
        
        self.pitch_surface = s
